import java.rmi.*;
import java.rmi.server.*;
import java.io.*;
import javax.swing.*;
import java.net.*;

public class MainServerImpl extends UnicastRemoteObject implements InterInterface, MainServerInterface
{
	static int s = 1;
	static int yes = 0;
	static int no = 0;
	static int one = 0;
	static int two = 0;
	static int three = 0;
	static int four = 0;
	
	
	
	public MainServerImpl() throws RemoteException {
	}
	public String ip_address(String ip,String roll,String state) throws RemoteException {
		t3.append(ip+"\t"+roll+"  "+ state +"\n");
		return "";
	}
	public String QuesSend(String ques) throws RemoteException
	{
		if(ques.equals("Rabbi1"))
		return (t.getText());
		else
		return null;
	}
	public String TimeSend(String samay) throws RemoteException
	{
		if(samay.equals("Rabbi2"))
		return (taf4.getText());
		else
		return null;
	}
	public String NumQues(String num) throws RemoteException
	{
		if(num.equals("Rabbi3"))
		return (taf5.getText());
		else
		return null;
	}
	public String AnsTransfer(String Ans,String roll) throws RemoteException
	{
		String dirname = "Ansfile/"+roll;
		String filename = "Script_"+s + ".txt";
		File dir = new File(dirname);
		dir.mkdir();
		File fina = new File(dir,filename);
		try
			{
				FileWriter out = new FileWriter(fina);
				String text = t.getText();
				
				out.write(text);
				out.close();
			}
			catch(IOException e)
			{
				e.printStackTrace();
			}
			s++;
		if(Ans.equals(""))
		return "File Not Found";
		else
		return "hollo";
	}
	public String ToQues(String tq)throws RemoteException
	{
		String rab = "";
		if(txa02.getText().equals(""))
		rab = tfield03.getText();
		else
		rab = txa02.getText();
		return rab;
	}
	
	public String[] McqQuestion(String mq)throws RemoteException
	{
		String ra[] = new String[500];
		String rab = "";
		for(int i=0;i<500;i++)
		{
			ra[i] = "";
		}
		try {
			if(txafile.getText().equals(""))
			rab = tfield01.getText();
			else
			rab = "mcq_ques/"+txafile.getText();
			
			FileReader filename = new FileReader(rab);
			BufferedReader dis = new BufferedReader(filename);
			int i = 0;
			String theline = "";
				while((theline = dis.readLine()) != null)
				{
				(ra[i] = theline);
				i++;
				}
				
								
			}
			catch(IOException ioe)
			{
				JOptionPane.showMessageDialog(null,"Error Openning the file","Error",JOptionPane.ERROR_MESSAGE);
			}
			return ra;
	}
	
	public String McqTime(String tim)throws RemoteException
	{
		String rab = "";
		if(txf39.getText().equals(""))
			rab = tfield02.getText();
			else
			rab = txf39.getText();
		
		return rab;
	}
	
	public String McqResult(String mr,String mroll)throws RemoteException
	{
		t8.append(mroll+"\t\t"+mr+"\n");
		t9.append(mroll+"\t\t"+mr+"\n");
		return "good";
	}
	
	public String VoteYesNoQuestion(String id)throws RemoteException
	{
		return t10.getText();
	}
	
	public String voteYesNoAnswer(String ans)throws RemoteException
	{
		if(ans.equals("Yes"))
		{
		yes++;
		vyes.setText(Integer.toString(yes));
		}
		if(ans.equals("No"))
		{
		no++;
		vno.setText(Integer.toString(no));
		}
		return null;
	}
	public String VoteSelectionQuestion(String voteid)throws RemoteException
	{
		return t111.getText();
	}
	
	public String VoteSelectionSect1(String ans)throws RemoteException
	{
		return txf42.getText();
	}
	
	public String VoteSelectionSect2(String ans)throws RemoteException
	{
		return txf43.getText();
	}
	
	public String VoteSelectionSect3(String ans)throws RemoteException
	{
		return txf44.getText();
	}
	
	public String VoteSelectionSect4(String ans)throws RemoteException
	{
		return txf45.getText();
	}
	public String VoteSelectionAnswer(String ans)throws RemoteException
	{
		if(ans.equals("1"))
		{
		one++;
		vone.setText(Integer.toString(one));
		}
		if(ans.equals("2"))
		{
		two++;
		vtwo.setText(Integer.toString(two));
		}
		if(ans.equals("3"))
		{
		three++;
		vthree.setText(Integer.toString(three));
		}
		if(ans.equals("4"))
		{
		four++;
		vfour.setText(Integer.toString(four));
		}
		return null;
	}
	
}
